/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.gui.exports;

import java.io.File;
import org.dafe.partyband.gui.exports.ExportVisualPanel1.Result;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.db.api.handlers.ISongResourceHandler;
import org.dafe.partyband.dochandler.OOActions;
import org.dafe.partyband.db.api.DbManager;
import org.dafe.partyband.gui.Utilities;
import org.netbeans.api.progress.ProgressHandle;
import org.openide.WizardDescriptor;
import org.openide.WizardDescriptor.Panel;

/**
 *
 * @author dafe
 */
public class ExportWizardIterator extends WizardDescriptor.ArrayIterator<WizardDescriptor> implements WizardDescriptor.ProgressInstantiatingIterator<WizardDescriptor> {
    private WizardDescriptor wd;

    public ExportWizardIterator(Panel<WizardDescriptor>[] array) {
        super(array);
    }

    public Set instantiate(ProgressHandle ph) throws IOException {
        Result result = (Result) wd.getProperty(ExportWizardPanel1.RESULT);
        
        ISongResourceHandler songResHandler = DbManager.getBandManager().getHandlersLookup().lookup(ISongResourceHandler.class);

        List<ISongResource> resources = result.resources;
        File exportDir = result.exportDir;
        List<String> fileNames = result.fileNames;

        ph.start(resources.size());
        int counter = 0;
        for (int i = 0; i < resources.size(); i++) {
            ISongResource res = resources.get(i);
            String oldFilePath = res.getFilePath();
            res.setFilePath(exportDir.getPath() + File.separator + fileNames.get(i));
            songResHandler.exportFromDbToFile(res);
            
            // TBD - delete originals in case of conversion
            if (result.convertTo != null && Utilities.isTextType(res)) {
                OOActions.convert(new File(res.getFilePath()), result.convertTo);
            }
            // set old name back
            res.setFilePath(oldFilePath);

            ph.progress(++counter);
        }

        ph.finish();

        return Collections.EMPTY_SET;
    }

    public void attachDescriptor (WizardDescriptor wd) {
        this.wd = wd;
    }

    public Set instantiate() throws IOException {
        throw new IllegalStateException("Should never be called");
    }

    public void initialize(WizardDescriptor wd) {
    }

    public void uninitialize(WizardDescriptor wd) {
    }




}
